package com.mobor.moborsvr.obj.category.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.mobor.moborsvr.infra.search.SearchRequestManager;
import com.mobor.moborsvr.infra.search.impl.SearchWorker;
import com.mobor.moborsvr.infra.util.ApplicationContextProviderUtil;
import com.mobor.moborsvr.obj.category.categories.CategoryObjectDO;
import com.mobor.moborsvr.obj.category.dao.CategoryObjectDao;
import com.mobor.moborsvr.obj.category.dao.CategoryObjectRepository;
import com.mobor.moborsvr.obj.category.service.CategoryObjectService;
import com.mobor.moborsvr.obj.constant.MoborObjectsMessages;

/**
 * Implements {@link CategoryObjectService} interface.
 * @author teroleg
 *
 */
public class CategoryObjectServiceImpl implements CategoryObjectService {
	
	private static final Logger logger = LogManager.getLogger(CategoryObjectServiceImpl.class.getName());
	
	@Autowired
	private SearchRequestManager searchRequestManager;
	@Autowired
	private CategoryObjectRepository categoryObjectReppository;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<CategoryObjectDO> getObjectCategories() {
		List<CategoryObjectDO> coList = new LinkedList<CategoryObjectDO>();
		Future<List<?>> results;
        try{
        	CategoryObjectDao cod = (CategoryObjectDao) ApplicationContextProviderUtil.getApplicationContext().getBean("categoryObjectDao");
        	logger.debug("CategoryObjectDao acquired.");
        	cod.setCoId(null);
        	SearchWorker sw = (SearchWorker) ApplicationContextProviderUtil.getApplicationContext().getBean("searchWorker");
        	logger.debug("Search worker acquired.");
			sw.setBaseDao(cod);
        	
        	results = searchRequestManager.executeSearch(sw);
        	logger.debug("Executing search.");
        	if(results.get() != null && !results.get().isEmpty())
        	for(Object o : results.get())
        		coList.add((CategoryObjectDO)o);
        	
        	logger.debug("Object categories list built successfully.");
        }
        catch ( InterruptedException e){
        	logger.error(MoborObjectsMessages.OBJ_E_OBJECT_CATEGORIES_SEARCH_INTERUPTED, e);
        }
        catch(ExecutionException e){
        	logger.error(MoborObjectsMessages.OBJ_E_OBJECT_CATEGORIES_SEARCH_FAILED, e);
        }
        
        return coList;
	}

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public CategoryObjectDO getObjectCategory(Long coId) {
		CategoryObjectDO co = null;
		Future<List<?>> results;
        try{
        	CategoryObjectDao cod = (CategoryObjectDao) ApplicationContextProviderUtil.getApplicationContext().getBean("categoryObjectDao");
        	logger.debug("CategoryObjectDao acquired.");
        	cod.setCoId(coId);
        	SearchWorker sw = (SearchWorker) ApplicationContextProviderUtil.getApplicationContext().getBean("searchWorker");
        	logger.debug("Search worker acquired.");
			sw.setBaseDao(cod);
        	
        	results = searchRequestManager.executeSearch(sw);
        	logger.debug("Executing search for id: " + coId);
        	if(results.get() != null && !results.get().isEmpty())
        		co = (CategoryObjectDO)results.get().get(0);
        	
        	logger.debug("Object categories list built successfully.");
        }
        catch ( InterruptedException e){
        	logger.error(MoborObjectsMessages.OBJ_E_OBJECT_CATEGORIES_SEARCH_INTERUPTED, e);
        }
        catch(ExecutionException e){
        	logger.error(MoborObjectsMessages.OBJ_E_OBJECT_CATEGORIES_SEARCH_FAILED, e);
        }
        
        return co;
	}

}
